package com.polaris.live.common.ext

import com.hjq.language.MultiLanguages
import com.polaris.live.common.appContext
import kotlin.math.floor

/**
 * Int的扩展函数
 *
 * @author Created by 半仙 on 2024/1/8/008 16:54
 */
fun Number?.formatNumberWithK(): String {
    if (this == null) return "0"

    val number = toLong()
    return when {
        number >= 1000 * 10000 -> {
            val formatted =
                floor(number / (100 * 10000.0) * 10) / 10  // 先乘以10，向下取整，再除以10，保留一位小数
            String.format(MultiLanguages.getSystemLanguage(appContext), "%.1fm", formatted)
        }

        number >= 10000 -> {
            val formatted = floor(number / 1000.0 * 10) / 10  // 类似地处理万位数
            String.format(MultiLanguages.getSystemLanguage(appContext), "%.1fk", formatted)
        }

        else -> this.toString()
    }.convertArabicDigitsToNormal()
}

fun Long.overtime(time: Long): Boolean {
    return System.currentTimeMillis() - this > time
}